package com.kolon.business.mServiceDesk_.controller;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.jdom.Document;
import org.jdom.Element;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.kolon.framework.constant.Constant;
import com.kolon.framework.interfaces.SimpleController;
import com.kolon.framework.util.ComUtil;
import com.kolon.framework.util.MsgUtil;

@Controller("com.kolon.business.mServiceDesk_.controller.MServiceDeskRequestViewController")
public class MServiceDeskRequestViewController implements SimpleController {

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@RequestMapping(value="/business/mServiceDesk_/mServiceDeskRequestView.do")
	public ModelAndView controller(HttpServletRequest request,
		HttpServletResponse response) throws Exception {
		// 도메인
		String url = MsgUtil.getMessage("xml.url");
		// 세션 아이디 및 암호 값을 불러옴
		HttpSession httpSession = request.getSession();
		Map userMap = (Map)httpSession.getAttribute(Constant.SESSION_USER_INFO);
		String id = (String)userMap.get("userId");
		String pw = (String)userMap.get("userPw");
		String page = ComUtil.nullChk(request.getParameter("page"), "1");
		String tid = id;
		//권한을 새션에 호출
		HttpSession session = request.getSession(true);
		String assignee = (String)session.getAttribute("SESSION_ASSIGNEE");
		String approval = (String)session.getAttribute("SESSION_APPROVAL");
		StringBuffer buffer = new StringBuffer();
		buffer.append(url);
		Map map = new HashMap();
			// 검색
			buffer.append("/servicedesk.aspx?mode=").append("3");
			buffer.append("&Ref_Num=").append(ComUtil.nullChk(request.getParameter("Ref_Num"),""));
			buffer.append("&userid=").append(tid);
			// XML DOCUMENT 및 인증 처리
			Document doc = ComUtil.getDocument(buffer.toString(), id, pw);
			Element element = doc.getRootElement();
			List xmlList = element.getChildren("Table");
			Iterator itXml = xmlList.iterator();
			// XML 데이터 
			if (itXml.hasNext()) {
				Element el = (Element)itXml.next();
				map = new HashMap();
				map.put("REF_NUM", el.getChild("REF_NUM").getText());
				map.put("REQ_AREA", el.getChild("REQ_AREA").getText());
				map.put("REQ_DESC", el.getChild("REQ_DESC").getText());
				map.put("OPEN_DATE", el.getChild("OPEN_DATE").getText());
				map.put("STATUS", el.getChild("STATUS").getText());
				map.put("PLAN_DATE", ComUtil.nullChk(el.getChild("PLAN_DATE").getText()));
				map.put("ASSIGNEE_NM", el.getChild("ASSIGNEE_NM").getText());
				if(el.getChild("STATUS").getText().equals("종료")){ //종료처리
					map.put("ASSIGNEE_INFO", el.getChild("ASSIGNEE_INFO").getText());
					map.put("DO_DESC", el.getChild("DO_DESC").getText());
					map.put("CLOSE_DATE", el.getChild("CLOSE_DATE").getText());
				}else{
					map.put("ASSIGNEE_INFO", "");
					map.put("DO_DESC", "");
					map.put("CLOSE_DATE", "");
				}
			}	
		// 리턴
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.addObject("viewMap", map);
		modelAndView.addObject("assignee", assignee);
		modelAndView.addObject("approval", approval);
		modelAndView.addObject("page", page);
		return modelAndView;
	}

}